home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / HYPERXCM / XCMDUTIL.C < prev    next >
Text File  |  1991-10-22  |  4KB  |  122 lines

  1. /*
  2.   ⌐ 1991 Dick Guertin
  3.   XCmd Utilities by Gary Bond & Sioux Lacy
  4. */
  5.  
  6. #include <MacTypes.h>
  7. #include <string.h>
  8. #include "HyperXCmd.h"
  9. #include "XCmdUtil.h"
  10.  
  11. /*----------------------------------------------------------------
  12. * This utility sets a return string for HyperCard.
  13. *---------------------------------------------------------------*/
  14.  
  15. void Fail (paramPtr, str)
  16.   XCmdBlockPtr paramPtr;
  17.   char *str;
  18. {
  19.   paramPtr->returnValue = (Handle) CopyStrToHand(str);
  20.   return;
  21. } /*- proc-end: Fail -*/
  22.  
  23. /*----------------------------------------------------------------
  24. * This utility returns the parameter count, error if not in range.
  25. *---------------------------------------------------------------*/
  26.  
  27. short GetParamCount (paramPtr, min, max)
  28.   XCmdBlockPtr paramPtr;
  29.   short min, max;
  30. { short count;  
  31.   count = paramPtr->paramCount;
  32.   if ((count > max) || (count < min))
  33.      count = -1;
  34.   return (count);
  35. } /*- proc-end: GetParamCount -*/
  36.  
  37. /*----------------------------------------------------------------
  38. * This utility returns the parameter count, error if not in range.
  39. *---------------------------------------------------------------*/
  40.  
  41. short CheckParamCount (paramPtr, amount)
  42.   XCmdBlockPtr paramPtr;
  43.   short amount;
  44. { return (GetParamCount(paramPtr, amount, amount));
  45. } /*- proc-end: CheckParamCount -*/
  46.  
  47. /*----------------------------------------------------------------
  48. * This utility allocates heapspace and copies string into it.
  49. *---------------------------------------------------------------*/
  50.  
  51. Handle CopyStrToHand (str)
  52.   char *str;
  53. { Handle newHndl;
  54.   newHndl = (Handle) NewHandle((long)(strlen(str) + 1));
  55.   strcpy((char*)(*newHndl), str);
  56.   return (newHndl);
  57. } /*- proc-end: CopyStrToHand -*/
  58.  
  59. /*----------------------------------------------------------------
  60. * This utility makes a callback to HyperCard to convert a string
  61. * to an unsigned long integer.  It take a handle to a C-string.
  62. *---------------------------------------------------------------*/
  63.  
  64. long HandleToNum (paramPtr, hndl)
  65.   XCmdBlockPtr paramPtr;
  66.   Handle hndl;
  67. { char str[32];
  68.   long num;
  69.   
  70.   strcpy(str, *hndl);
  71.   num = StrToLong(paramPtr, (Str31*)ToPstr(str));
  72.   return (num);
  73. } /*- proc-end: HandleToNum -*/
  74.  
  75.  
  76. /*----------------------------------------------------------------
  77. * This utility copies the string pointed to by a handle into a
  78. * character array, converts it from C to Pascal format.
  79. * Note that the C string is overwritten by the Pascal string.
  80. *---------------------------------------------------------------*/
  81.  
  82. void HandleToPstr (str, hndl)
  83.   Str255 str;
  84.   Handle hndl;
  85. {
  86.   strcpy((char*)str, *hndl);
  87.   ToPstr((char*)str);
  88. } /*- proc-end: HandleToPstr -*/
  89.  
  90. /*----------------------------------------------------------------
  91. * This utility converts a Pascal string to a C string.
  92. * Note that the Pascal string is overwritten in the process.
  93. *---------------------------------------------------------------*/
  94.  
  95. char* ToCstr (str)
  96. char *str;
  97. { unsigned char length, i;
  98.  
  99.   length = str[0];
  100.   for (i=0; i<=length; ++i)
  101.     str[i] = str[i+1];  /*- Move string left -*/
  102.   str[length] = '\0';
  103.   return (str);
  104. } /*- proc-end: ToCstr -*/
  105.  
  106. /*----------------------------------------------------------------
  107. * This utility converts a C string to a Pascal string.
  108. * Note that the C string is overwritten in the process.
  109. *---------------------------------------------------------------*/
  110.  
  111. char* ToPstr (str)
  112. char *str;
  113. { unsigned char length, i;
  114.  
  115.   for (i=0; str[i]!=0; ++i);
  116.   length = i;
  117.   while (i--)
  118.     str[i+1] = str[i];  /*- Move string right -*/
  119.   str[0] = length;
  120.   return (str);
  121. } /*- proc-end: ToPstr -*/
  122.